package com.example.shoppingdemo.goods.MessageAdpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.shoppingdemo.R;
import com.example.shoppingdemo.goods.model.UsersMsgModel;

import java.util.List;

public class MessgeAdapter extends RecyclerView.Adapter<MessgeAdapter.MessgeHoHolder> {

    private Context context;
    private List<UsersMsgModel> usersMsgModels;
    private onMessgeLister lister;
    public void setMessgeModel(List<UsersMsgModel> data){
        this.usersMsgModels = data;
    }
    public MessgeAdapter(Context context){
        this.context = context;
    }
    public void setMessgeLister(onMessgeLister lister){
        this.lister = lister;
    }
    @NonNull
    @Override
    public MessgeHoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View msgView;
        msgView = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_list, parent, false);
        MessgeHoHolder holder = new MessgeHoHolder(msgView);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MessgeHoHolder holder, int position) {
        holder.useImg.setImageResource(usersMsgModels.get(position).getImge());
        holder.name.setText(usersMsgModels.get(position).getName());
        holder.address.setText(usersMsgModels.get(position).getAddress());
        holder.mesg.setText(usersMsgModels.get(position).getMessage());
        holder.time.setText(usersMsgModels.get(position).getData());
        holder.useImgs.setImageResource(usersMsgModels.get(position).getImges());
    }

    @Override
    public int getItemCount() {
        return null==usersMsgModels?0:usersMsgModels.size();
    }

    class MessgeHoHolder extends RecyclerView.ViewHolder{
        ImageView useImg,useImgs;
        TextView name,address,mesg,time;

        public MessgeHoHolder(@NonNull View itemView) {
            super(itemView);
            useImg = itemView.findViewById(R.id.msg_user_head);
            name = itemView.findViewById(R.id.msg_user_name);
            address = itemView.findViewById(R.id.msg_user_adress);
            mesg = itemView.findViewById(R.id.msg_list_msg);
            time = itemView.findViewById(R.id.msg_data);
            useImgs = itemView.findViewById(R.id.msg_imge_list);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    lister.onMessgeResult(MessgeHoHolder.this.getAdapterPosition());
                }
            });
        }
    }
    public interface onMessgeLister{
        void onMessgeResult(int position);
    }
}
